var leaveInfo = require("../model/leaveTable");

var leave = {
  //leave的增删改查
  add: (info) => {
    return leaveInfo.insertMany([info]); //返回的是一个promise
  },
  // query: (info) => {
  //   return leaveInfo.find(info); //返回的是一个promise
  // },
  update: (method, content) => {
    //更新的接口  需要两个对象
    return leaveInfo.updateOne(method, content);
  },
  group: (key) => {
    //根据key进行来分组统计
    return leaveInfo.aggregate([
      {
        $group: {
          _id: "$" + key, //_id不能变  可以是username 也可以是type
          count: { $sum: 1 }, //类似于.count 但这是是管道函数　　所以还需要加上$sum关键词
        },
      },
    ]);
  },
  querycount: (params) => leaveInfo.find(params).count(),
  query: (params, skip, limit) =>
    leaveInfo.find(params).skip(skip).limit(limit), //跳过多少条数据  显示多少条数据
};

module.exports = leave;
